﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data.SqlClient;
using System.Data;
using Microsoft.SqlServer.Server;

public partial class admin_Default : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        string connStr = System.Configuration.ConfigurationManager.ConnectionStrings["connStr"].ConnectionString;
        SqlConnection conn = new SqlConnection(connStr);
        conn.Open();
        SqlDataAdapter da = new SqlDataAdapter("select * from KhachHang where Roles='users'",conn);
        DataTable dt = new DataTable();
        da.Fill(dt);
        GridView1.DataSource = dt;
        GridView1.DataBind();
    }
    protected void GridView1_SelectedIndexChanged(object sender, EventArgs e)
    {
        string connStr = System.Configuration.ConfigurationManager.ConnectionStrings["connStr"].ConnectionString;
        SqlConnection conn = new SqlConnection(connStr);
        conn.Open();
        btnClear.Enabled = true;
        txtLDBan.Text = "";
        txtSLBan.Text = "";
        if (GridView1.SelectedRow.Cells[3].Text == "False")
        {
            btnUnban.Enabled = false;
            btnBan.Enabled = true;
            txtSLBan.Enabled = true;
            txtLDBan.Enabled = true;
        }
        else {
            btnUnban.Enabled = true;
            btnBan.Enabled = false;
            txtSLBan.Enabled = false;
            txtLDBan.Enabled = false;
            SqlCommand cmd = new SqlCommand("select * from KhachHang where MaCus = " + GridView1.SelectedRow.Cells[1].Text,conn);
            SqlDataReader dr = cmd.ExecuteReader();
            dr.Read();
            txtLDBan.Text = dr.GetString(12);
        }
        conn.Close();
    }
    protected void btnClear_Click(object sender, EventArgs e)
    {
        txtLDBan.Text = "";
        txtSLBan.Text = "";
        txtSLBan.Enabled = false;
        txtLDBan.Enabled = false;
        btnBan.Enabled = false;
        btnUnban.Enabled = false;
        GridView1.SelectedIndex = -1;
        btnClear.Enabled = false;
    }
    protected void btnBan_Click(object sender, EventArgs e)
    {
        //String sqlDateTime = DateTime.Now.Date.ToString();
        //Response.Write("<script>alert('" + sqlDateTime + "')</script>");

        string connStr = System.Configuration.ConfigurationManager.ConnectionStrings["connStr"].ConnectionString;
        SqlConnection conn = new SqlConnection(connStr);
        conn.Open();
        double banDays;
        if (!Double.TryParse(txtSLBan.Text, out banDays))
        {
            Response.Write("<script>alert('Xin vui long nhap so ngay ban!')</script>");
            return;
        }
        else
        {
            SqlCommand cmd = new SqlCommand("update KhachHang set Banned = 1, BanFromDate = '" + DateTime.Now + "', BanToDate ='" + DateTime.Now.AddDays(banDays) + "', LyDoBan = '"+txtLDBan.Text+"' where MaCus = " + GridView1.SelectedRow.Cells[1].Text);
            cmd.Connection = conn;
            cmd.ExecuteNonQuery();
            btnBan.Enabled = false;
            btnUnban.Enabled = false;
            GridView1.SelectedIndex = -1;
            btnClear.Enabled = false;
            SqlDataAdapter da = new SqlDataAdapter("select * from KhachHang where Roles='users'", conn);
            DataTable dt = new DataTable();
            da.Fill(dt);
            GridView1.DataSource = dt;
            GridView1.DataBind();
            txtSLBan.Text = "";
            txtLDBan.Text = "";
            txtSLBan.Enabled = false;
            txtLDBan.Enabled = false;
        }
        conn.Close();
    }
    protected void btnUnban_Click(object sender, EventArgs e)
    {
        string connStr = System.Configuration.ConfigurationManager.ConnectionStrings["connStr"].ConnectionString;
        SqlConnection conn = new SqlConnection(connStr);
        conn.Open();
        SqlCommand cmd = new SqlCommand("update KhachHang set Banned = 0, BanFromDate = null, BanToDate = null, LyDoBan = null where MaCus = " + GridView1.SelectedRow.Cells[1].Text);
        cmd.Connection = conn;
        cmd.ExecuteNonQuery();
        btnBan.Enabled = false;
        btnUnban.Enabled = false;
        GridView1.SelectedIndex = -1;
        btnClear.Enabled = false;
        SqlDataAdapter da = new SqlDataAdapter("select * from KhachHang where Roles='users'", conn);
        DataTable dt = new DataTable();
        da.Fill(dt);
        GridView1.DataSource = dt;
        GridView1.DataBind();
        conn.Close();
    }
    protected void GridView1_PageIndexChanging(object sender, GridViewPageEventArgs e)
    {
        GridView1.PageIndex = e.NewPageIndex;
        GridView1.DataBind();
    }
}